﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Text;
using System.Xml;
using TouchUI;
using GoogleVoice;

namespace GVVVM
{
    public class MainDialog : Dialog
    {
        private ScrollList scrollList;
        private int headerHeight;
        public int SelectedLine { get; set; }

        public MainDialog(int screenFactor, Rectangle r, Connection gvconn)
        {
            Text = "Voicemail";
            ScreenFactor = screenFactor;
            Rectangle = r;
            if(Common.Instance.TouchDevice)
                headerHeight = 24 * screenFactor;
            scrollList = new ScrollList()
            {
                Rectangle = new Rectangle(0, headerHeight, r.Width, r.Height - headerHeight),
                ListItemHeight = 44 * screenFactor,
                ScreenFactor = screenFactor,
                HighBrush = Common.Instance.HighBrush,
                GrayBrush = Common.Instance.GrayBrush
            };
            int i = 0;
            foreach(VoiceMail thisVoicemail in gvconn.MyVoiceMails)
            {

                scrollList.ListItems.Add(new MainDialogListItem(i + 1, ScreenFactor, thisVoicemail ));
            scrollList.ScrollHeight = scrollList.ListItemHeight * scrollList.ListItems.Count;
            HasScrollControl = true;
            scrollList.Selected += new EventHandler(scrollList_Selected);
                i++;
            }
        }

        #region Events
        public event EventHandler Selected;
        protected virtual void OnSelected(EventArgs e)
        {
            if(Selected != null)
                Selected(this, e);
        }
        #endregion

        private void scrollList_Selected(object sender, EventArgs e)
        {
            SelectedLine = scrollList.SelectedLine;
            OnSelected(e);
        }

        public override void Paint(Graphics g)
        {
            g.Clear(BackColor);
            scrollList.Paint(g);
            if(headerHeight > 0)
            {
                g.FillRectangle(Common.Instance.BackBrush, 0, 0, Rectangle.Width, headerHeight);
                g.DrawString(Text, Common.Instance.Font, Common.Instance.HighBrush, 8 * ScreenFactor, 5 * ScreenFactor);
                g.DrawLine(Common.Instance.Pen, 0, headerHeight - 1, Rectangle.Width, headerHeight - 1);
            }
        }

        public override void MouseDown(Point p)
        {
            scrollList.MouseDown(new Point(p.X, p.Y - headerHeight));
        }

        public override void MouseMove(Point p)
        {
            scrollList.MouseMove(new Point(p.X, p.Y - headerHeight));
        }

        public override void MouseUp(Point p)
        {
            scrollList.MouseUp(new Point(p.X, p.Y - headerHeight));
        }

        public override void KeyDown(KeyEventArgs e)
        {
            scrollList.KeyDown(e);
        }

        public override void KeyUp(KeyEventArgs e)
        {
            scrollList.KeyUp(e);
            SelectedLine = scrollList.SelectedLine;
        }

        public override void Resize(Rectangle r)
        {
            this.Rectangle = r;
            scrollList.Rectangle = new Rectangle(0, headerHeight, r.Width, r.Height - headerHeight);
        }
    }
}
